package legumes.client.ui.otherboxes.order;

import legumes.client.evt.customer.CustomerOrderChangedEvent;
import legumes.client.evt.customer.CustomerOrderChangedEventHandler;
import legumes.client.evt.manager.EventBusManager;
import legumes.client.resources.PNResources;
import legumes.client.ui.widgets.utils.Formatters;
import legumes.shared.domain.MenuCount;
import legumes.shared.domain.OrderPart;
import legumes.shared.domain.SellSessionForCustomer;

import com.google.gwt.user.client.ui.FlexTable;
import com.smartgwt.client.widgets.WidgetCanvas;
import com.smartgwt.client.widgets.Window;
import com.smartgwt.client.widgets.events.CloseClickHandler;
import com.smartgwt.client.widgets.events.CloseClientEvent;
import com.smartgwt.client.widgets.layout.VLayout;

public class OrderViewWindow extends Window {

	public OrderViewWindow(SellSessionForCustomer sellSessionForCustomer_, final OrderPart op_) {
		super();
		setWidth(510);
		setHeight(480);
		
		setTitle("<img src=\""
				+ PNResources.INSTANCE.viewDetailsIcon().getURL()
				+ "\" />&nbsp;D&eacute;tails de la commande");

		setShowCloseButton(true);
		setShowMinimizeButton(false);
		setCanDrag(true);
		setCanDragReposition(true);
		setIsModal(true);
		setShowModalMask(true);  
		setAutoCenter(true);
		addCloseClickHandler(new CloseClickHandler() {  
            public void onCloseClick(CloseClientEvent event) {  
                destroy();  
            }  
        });
		
		float menuPrice = 0.0f;
		if(	op_.getOrderPartMenuItem().getMenuCountAsEnum().equals(MenuCount.ONE_MENU) ){
			menuPrice = sellSessionForCustomer_.getStall().getMenu().getWholePrice();
		} else if ( op_.getOrderPartMenuItem().getMenuCountAsEnum().equals(MenuCount.HALF_MENU)){
			menuPrice = sellSessionForCustomer_.getStall().getMenu().getHalfPrice();
		}
		final FlexTable ft = new FlexTable();
		ft.setStyleName("txtAlignCenter", true);
		ft.setHTML(0, 0, "<div class=\"detailTableCell\">Nb panier(s) : </div>");
		ft.setHTML(0, 1, "<div class=\"detailTableCell\">" + op_.getOrderPartMenuItem().getMenuCountAsEnum().getDisplayName() + "</div>");
		ft.setHTML(0, 2, "<div class=\"detailTableCell txtAlignRight\">" + Formatters.formatPrice(menuPrice, true) + "</div>");
		
		ft.setHTML(1, 0, "<div class=\"detailTableCell\">Hors panier : </div>");
		ft.setHTML(1, 1, "<div class=\"detailTableCell\">" + op_.getOrderPartProductItems().size() + " produit(s)</div>");
		ft.setHTML(1, 2, "<div class=\"detailTableCell\">" + Formatters.formatPrice(op_.computePriceWithoutMenu().getValue(), true) + "</div>");
		
		ft.setHTML(2, 0, "<div class=\"detailTableCell detailTotalLabel\">TOTAL : </div>");
		ft.setHTML(2, 1, "");
		ft.setHTML(2, 2, "<div class=\"detailTableCell detailTotalLabel txtAlignRight\">" + Formatters.formatPrice(op_.computePrice().getValue(), true) + "</div>");
		ft.getRowFormatter().setStyleName(2, "detailTotalRow");
		
		
		
		
		EventBusManager.getInstance().getEventBus().addHandler(
				CustomerOrderChangedEvent.TYPE, 
				new CustomerOrderChangedEventHandler(){

					@Override
					public void onOrderChanged(CustomerOrderChangedEvent event) {
						ft.setHTML(1, 1, "<div class=\"detailTableCell\">" + op_.getOrderPartProductItems().size() + " produit(s)</div>");
						ft.setHTML(1, 2, "<div class=\"detailTableCell\">" + Formatters.formatPrice(op_.computePriceWithoutMenu().getValue(), true) + "</div>");
						ft.setHTML(2, 2, "<div class=\"detailTableCell detailTotalLabel txtAlignRight\">" + Formatters.formatPrice(op_.computePrice().getValue(), true) + "</div>");
					}
				});

		CustomerOrderPanel cop = new CustomerOrderPanel();
		cop.initData(op_);
		
		VLayout mainLayout = new VLayout();
		mainLayout.setMargin(5);
		WidgetCanvas wc = new WidgetCanvas(ft);
		wc.setAutoHeight();
		mainLayout.setMembers(wc, cop);
		
		addItem(mainLayout);
		show();
	}
	
}
